﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Management;

namespace RemoteRunCommand
{
    public partial class Main : Form
    {
        const string process_name = "iisreset";
        string Username;
        string Password;
        string remote_address;

        public Main()
        {
            InitializeComponent();
        }

        private void resetControls()
        {
            Application.UseWaitCursor = false;
            toolStripStatusLabel1.Text = "";
            Status_timer.Enabled = false;
        }
        
        private void Run_button_Click(object sender, EventArgs e)
        {
            Username = textBoxUserName.Text;
            Password = textBoxPassword.Text;
            remote_address = textBoxComputerName.Text;
            //
            var processToRun = new[] { process_name };
            var connection = new ConnectionOptions();
            connection.Username = Username;
            connection.Password = Password;
            try
            {
                var wmiScope = new ManagementScope(String.Format("\\\\{0}\\root\\cimv2", remote_address), connection);
                var wmiProcess = new ManagementClass(wmiScope, new ManagementPath("Win32_Process"), new ObjectGetOptions());
                wmiProcess.InvokeMethod("Create", processToRun);
            }
            catch (Exception ex)
            {
                resetControls();
                MessageBox.Show(ex.Message, "Connection error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            toolStripStatusLabel1.Text = "Running...";
            Status_timer.Enabled = true;
            Application.UseWaitCursor = true;
        }

        private void Cancel_button_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void Status_timer_Tick(object sender, EventArgs e)
        {
            ConnectionOptions connectoptions = new ConnectionOptions();

            connectoptions.Username = Username;
            connectoptions.Password = Password;

            //IP Address of the remote machine
            string ipAddress = remote_address;
            ManagementScope scope = new ManagementScope(@"\\" + ipAddress + @"\root\cimv2");
            scope.Options = connectoptions;
            //Define the WMI query to be executed on the remote machine
            SelectQuery query = new SelectQuery("select * from Win32_Process");

            using (ManagementObjectSearcher searcher = new
                        ManagementObjectSearcher(scope, query))
            {
                bool process_is_found = false;
                ManagementObjectCollection collection = searcher.Get();
                foreach (ManagementObject process in collection)
                {
                    if (process["Name"].ToString() == (process_name + ".exe")) 
                    {
                        process_is_found = true;
                    }
                }
                if (!process_is_found)
                {
                    resetControls();
                    MessageBox.Show("IIS has restarted", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }

            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //textBoxUserName.Text = "Administrator";
            //textBoxPassword.Text = "Qwerty1";
            //textBoxComputerName.Text = "10.0.1.176";
        }
    }
}
